package cn.iocoder.yudao.module.erp.controller.admin.suppliersendorder;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.suppliersendorder.vo.SupplierSendOrderPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.suppliersendorder.vo.SupplierSendOrderRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.suppliersendorder.vo.SupplierSendOrderSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.suppliersendorderitem.vo.SupplierVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.suppliersendorder.SupplierSendOrderDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.suppliersendorderitem.SupplierSendOrderItemDO;
import cn.iocoder.yudao.module.erp.service.suppliersendorder.SupplierSendOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP 随货同行单")
@RestController
@RequestMapping("/erp/supplier-send-order")
@Validated
public class SupplierSendOrderController {

    @Resource
    private SupplierSendOrderService supplierSendOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 随货同行单")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:create')")
    public CommonResult<Long> createSupplierSendOrder(@Valid @RequestBody SupplierSendOrderSaveReqVO createReqVO) {
        return success(supplierSendOrderService.createSupplierSendOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 随货同行单")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:update')")
    public CommonResult<Boolean> updateSupplierSendOrder(@Valid @RequestBody SupplierSendOrderSaveReqVO updateReqVO) {
        supplierSendOrderService.updateSupplierSendOrder(updateReqVO);
        return success(true);
    }

    @PutMapping("/print")
    @Operation(summary = "更新ERP 随货同行单")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:update')")
    public CommonResult<Boolean> printSupplierSendOrder(@Valid @RequestBody SupplierSendOrderSaveReqVO updateReqVO) {
        supplierSendOrderService.printSupplierSendOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 随货同行单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:delete')")
    public CommonResult<Boolean> deleteSupplierSendOrder(@RequestParam("id") String id) {
        supplierSendOrderService.deleteSupplierSendOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 随货同行单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:query')")
    public CommonResult<SupplierSendOrderRespVO> getSupplierSendOrder(@RequestParam("id") String id) {
        SupplierSendOrderDO supplierSendOrder = supplierSendOrderService.getSupplierSendOrder(id);
        return success(BeanUtils.toBean(supplierSendOrder, SupplierSendOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 随货同行单分页")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:query')")
    public CommonResult<PageResult<SupplierSendOrderRespVO>> getSupplierSendOrderPage(@Valid SupplierSendOrderPageReqVO pageReqVO) {
        PageResult<SupplierSendOrderDO> pageResult = supplierSendOrderService.getSupplierSendOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplierSendOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 随货同行单 Excel")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSupplierSendOrderExcel(@Valid SupplierSendOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplierSendOrderDO> list = supplierSendOrderService.getSupplierSendOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 随货同行单.xls", "数据", SupplierSendOrderRespVO.class,
                        BeanUtils.toBean(list, SupplierSendOrderRespVO.class));
    }
//    根据订单号数组获得随货同行单项列表
    @GetMapping("/list-by-out-order-ids")
    public CommonResult<List<SupplierSendOrderRespVO>> getSupplierSendOrderItemListByPurchaseOrderIds(@RequestParam("purchaseOrderIds") String[] purchaseOrderIds) {
        List<SupplierSendOrderRespVO> supplierSendOrderItemList = supplierSendOrderService.getSupplierSendOrderItemListByPurchaseOrderIds(purchaseOrderIds);
        return success(supplierSendOrderItemList);
    }

    //    根据入库编号 查询随货同行单列表 inId
    @GetMapping("/list-by-in-id")
    public CommonResult<List<SupplierSendOrderRespVO>> getSupplierSendOrderListByInId(@RequestParam("inId") String inId) {
        List<SupplierSendOrderRespVO> supplierSendOrderList = supplierSendOrderService.getSupplierSendOrderListByInId(inId);
        return success(supplierSendOrderList);
    }

    // ==================== 子表（ERP 随货同行单项） ====================

    @GetMapping("/supplier-send-order-item/list-by-out-order-id")
    @Operation(summary = "获得ERP 随货同行单项列表")
    @Parameter(name = "outOrderId", description = "随货同行单编号")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:query')")
    public CommonResult<List<SupplierSendOrderItemDO>> getSupplierSendOrderItemListByOutOrderId(@RequestParam("outOrderId") String outOrderId) {
        return success(supplierSendOrderService.getSupplierSendOrderItemListByOutOrderId(outOrderId));
    }

    @GetMapping("/supplier-send-order-item/getItemListByOutOrderId")
    @Operation(summary = "获得采购入库审批单详情数据")
    @Parameter(name = "outOrderId", description = "随货同行单编号")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:query')")
    public CommonResult<List<SupplierSendOrderItemDO>> getItemListByOutOrderId(@RequestParam("outOrderId") String outOrderId) {
        return success(supplierSendOrderService.getItemListByOutOrderId(outOrderId));
    }

    @GetMapping("/supplier-send-order-item/page")
    @Operation(summary = "获得ERP 随货同行单项分页")
    @Parameter(name = "outOrderId", description = "随货同行单编号")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:query')")
    public CommonResult<PageResult<SupplierSendOrderItemDO>> getSupplierSendOrderItemPage(PageParam pageReqVO,
                                                                                          @RequestParam("outOrderId") String outOrderId) {
        return success(supplierSendOrderService.getSupplierSendOrderItemPage(pageReqVO, outOrderId));
    }

    @PostMapping("/supplier-send-order-item/create")
    @Operation(summary = "创建ERP 随货同行单项")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:create')")
    public CommonResult<Long> createSupplierSendOrderItem(@Valid @RequestBody SupplierSendOrderItemDO supplierSendOrderItem) {
        return success(supplierSendOrderService.createSupplierSendOrderItem(supplierSendOrderItem));
    }

    @PutMapping("/supplier-send-order-item/update")
    @Operation(summary = "更新ERP 随货同行单项")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:update')")
    public CommonResult<Boolean> updateSupplierSendOrderItem(@Valid @RequestBody SupplierSendOrderItemDO supplierSendOrderItem) {
        supplierSendOrderService.updateSupplierSendOrderItem(supplierSendOrderItem);
        return success(true);
    }

    @DeleteMapping("/supplier-send-order-item/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除ERP 随货同行单项")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:delete')")
    public CommonResult<Boolean> deleteSupplierSendOrderItem(@RequestParam("id") Long id) {
        supplierSendOrderService.deleteSupplierSendOrderItem(id);
        return success(true);
    }

    @GetMapping("/supplier-send-order-item/get")
    @Operation(summary = "获得ERP 随货同行单项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:query')")
    public CommonResult<SupplierSendOrderItemDO> getSupplierSendOrderItem(@RequestParam("id") Long id) {
        return success(supplierSendOrderService.getSupplierSendOrderItem(id));
    }

    @GetMapping("/getSupplierSendOrderItemListByPurchaseOrderId")
    @Operation(summary = "获得随货同行单项列表")
    @Parameter(name = "purchaseOrderId", description = "订单编号")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:query')")
    public CommonResult<List<SupplierSendOrderItemDO>> getSupplierSendOrderItemListByPurchaseOrderId(@RequestParam("purchaseOrderId") String purchaseOrderId) {
        return success(supplierSendOrderService.getSupplierSendOrderItemListByPurchaseOrderId(purchaseOrderId));
    }

    @GetMapping("/getSupplierSendOrderItemPrintListByPurchaseOrderId")
    @Operation(summary = "获得随货同行单项列表")
    @Parameter(name = "purchaseOrderId", description = "订单编号")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order:query')")
    public CommonResult<List<SupplierSendOrderItemDO>> getSupplierSendOrderItemPrintListByPurchaseOrderId(@RequestParam("purchaseOrderId") String purchaseOrderId) {
        return success(supplierSendOrderService.getSupplierSendOrderItemPrintListByPurchaseOrderId(purchaseOrderId));
    }
}